package controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bizlogic.Manager;

import model.FacilitiesDTO;
import DAO.DAOFactory;
import DAO.FacilitiesDAO;
import DAO.NotFoundException;

/**
 * Servlet implementation class FacilityController
 */
@WebServlet("/FacilityController")
public class FacilityController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public FacilityController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}
	private void doProcess(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException {
		Manager m=new Manager();
		FacilitiesDTO facility=new FacilitiesDTO();
		String facilityId=req.getParameter("Id");
		System.out.println(req.getParameter("Id"));
		String testId = req.getParameter("Id");
		facility.setFacilityID(Integer.parseInt(testId));
		facility.setFacilityName(req.getParameter("name"));
		facility.setType(req.getParameter("type"));
		facility.setDescription(req.getParameter("description"));
		facility.setCapacity(Integer.parseInt(req.getParameter("capacity")));
		String ins = (String) req.getParameter("ins");
		
		boolean isvalid = false;
		
		if (ins.equalsIgnoreCase("true")) 
		{  
			if((m.Isvalid(facilityId))==true){
				m.insert(facility);
				isvalid = true;
			}
			else  {
				ins="true";
				req.setAttribute("insert", ins);
				req.setAttribute("error", "*Please enter a valid ID.");
				req.setAttribute("name",facility.getFacilityName());
				req.setAttribute("type",facility.getType());
				req.setAttribute("description",facility.getDescription());
				req.setAttribute("capacity",facility.getCapacity());
				req.setAttribute("Id",facility.getFacilityID());
				RequestDispatcher rd=req.getRequestDispatcher("setupPage.jsp");
				rd.forward(req, res);
				System.out.print("error");
			}
		} 
		else if(ins.equalsIgnoreCase("false")) {
			System.out.println("facility" + facility);
			m.update(facility);
			isvalid = true;
		}
		
		if ( isvalid ) {
			ArrayList<FacilitiesDTO> data=m.FindAllFacilities();
			req.setAttribute("facilities",data);
			RequestDispatcher rd=req.getRequestDispatcher("searchFacilities.jsp");
			try {
				rd.forward(req, res);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				req.setAttribute("error", "*Input ID already existed.");
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}

}


